class window.game_state_vm
  constructor: (@parent) ->
    @image = ko.observable ''
    @captions = ko.observableArray []
    @winner = ko.observable ''
    @game_number = ko.observable 0  #set when the game is created
    
    @_Create_Captions()
  
  # Creates captions submitted by other players
  _Create_Captions: =>
    underlying_array = @captions()
    underlying_array.push new caption_model("Darth_Vader", "No.  I am your father.")
    underlying_array.push new caption_model("GrumpyCat1337", "Meh.")
    underlying_array.push new caption_model("Brian", "One does not simply do the Harlem Shake")
    @captions.valueHasMutated()
  
  Play: (d, e) =>
    #set the current game in the parent to this game
    @parent.current_game this
    user_name = @parent.options.user_name()
    
    # Navigate to the next step depending on the current game state
    if @image() == ''     
      @parent.Navigate(Templates.SELECT_IMAGE)
    else if !(@captions().some (caption) -> caption.user_name() is user_name)
      @parent.captionImage.selectedImage(@image())
      console.log "Current Game Image Set To #{@parent.captionImage.selectedImage()}"
      @parent.Navigate(Templates.CAPTION_IMAGE)
    else if @winner() == ''
      @parent.Navigate(Templates.VOTE)
    else
      @parent.Navigate(Templates.VOTING_RESULTS)